package org.apache.commons.graph.algorithms.coloring

import org.apache.commons.graph.utils.Assertions

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
 * Maintains the color for each vertex and the required number of colors for [org.apache.commons.graph.Graph] coloring.
 *
 * @param <V> the Graph vertices type.
 * @param <C> the Color type.
</C></V> */
/**
 * This class can be instantiated only inside the package
 */
class ColoredVertices<V, C> internal constructor() {

    private val coloredVertices: MutableMap<V, C> = HashMap()

    private val usedColor: MutableList<C> = ArrayList()

    /**
     * Store the input vertex color.
     *
     * @param v the vertex for which storing the color.
     * @param color the input vertex color.
     */
    fun addColor(v: V, color: C) {
        coloredVertices[v] = color
        val idx = usedColor.indexOf(color)
        if (idx == -1) {
            usedColor.add(color)
        } else {
            usedColor[idx] = color
        }
    }

    /**
     * Tests if the 'vertex' is colored.
     *
     * @param vertex the vertex
     * @return true if the colored vertex is contained into the map, false otherwise
     */
    fun containsColoredVertex(vertex: V): Boolean {
        return coloredVertices.containsKey(vertex)
    }

    /**
     * Returns the color associated to the input vertex.
     *
     * @param v the vertex for which getting the color.
     * @return the color associated to the input vertex.
     */
    fun getColor(v: V): C? {
        //var v = v
        //v = Assertions.checkNotNull(v, "Impossible to get the color for a null Vertex")

        return coloredVertices[v]
    }

    val requiredColors: Int
        /**
         * Returns the number of required colors for coloring the Graph.
         *
         * @return the number of required colors for coloring the Graph.
         */
        get() = usedColor.size

    /**
     * Remove the input vertex color.
     *
     * @param v the vertex for which storing the color.
     */
    fun removeColor(v: V) {
        val color = coloredVertices.remove(v)
        usedColor.remove(color)
    }
}
